package com.ym.jiujiangeyou;

import org.json.JSONException;

import org.json.JSONObject;

import com.google.gson.JsonObject;

import com.ym.jiujiangeyou.app.AppManager;
import com.ym.jiujiangeyou.app.MyApplication;
import com.ym.jiujiangeyou.bitmap.SimpleImageLoad;
import com.ym.jiujiangeyou.entity.Info;
import com.ym.jiujiangeyou.ui.DialogView;
import com.ym.jiujiangeyou.util.Constants;
import com.ym.jiujiangeyou.util.HttpUtil;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 */
public class IntroduceActivity extends Activity implements OnClickListener {

	private TextView title_text, city_introduct_tv;
	private LinearLayout back_layout, linearlayout;
	private int width;
	private int height;
	private Dialog dialog;
	private Toast toast;
	SimpleImageLoad simpleImageLoad;
	Info info = null;
	ImageView iv;
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case Constants.HTTP_REQUEST_ERROR:
				toast = DialogView.popToast(IntroduceActivity.this,
						R.string.http_connect_error);
				toast.show();
				break;
			case Constants.HTTP_REQUEST_SUCCESS:
				info = (Info) msg.obj;
				city_introduct_tv.setText(info.getIntroduction());
				simpleImageLoad.DisplayImage(info.getUrl(), iv);
				dialog.dismiss();
				break;
			default:
				break;
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
		super.setContentView(R.layout.city_introduce);

		title_text = (TextView) findViewById(R.id.title_text);
		back_layout = (LinearLayout) findViewById(R.id.title_back);
		city_introduct_tv = (TextView) findViewById(R.id.city_introduct_tv);
		linearlayout = (LinearLayout) this.findViewById(R.id.linearlayout);
		iv = (ImageView) findViewById(R.id.lushan_pic);
		simpleImageLoad = new SimpleImageLoad(this);
		// 等到屏幕的大小
		/*
		 * width = this.getWindowManager().getDefaultDisplay().getWidth();
		 * height = this.getWindowManager().getDefaultDisplay().getHeight();
		 * height = (height / 4) + 50; LinearLayout.LayoutParams layoutParams =
		 * (LinearLayout.LayoutParams) linearlayout .getLayoutParams();
		 * layoutParams.width = width; layoutParams.height = height;
		 * linearlayout.setLayoutParams(layoutParams); LinearLayout.LayoutParams
		 * mParams = new LinearLayout.LayoutParams(
		 * LinearLayout.LayoutParams.WRAP_CONTENT,
		 * LinearLayout.LayoutParams.WRAP_CONTENT); mParams.setMargins(10, 0,
		 * 10, 0) ; iv = new ImageView(this) ; iv.setLayoutParams(mParams);
		 */

		title_text.setText(R.string.title_text_introduce);
		back_layout.setOnClickListener(this);
		dialog = DialogView.popDialog(this, R.string.isloading);
		dialog.show();

		new Thread(new Runnable() {
			Message msg = new Message();

			@Override
			public void run() {
				// TODO Auto-generated method stub
				String s = HttpUtil.httpGet(Constants.GET_INTRODUCTION_INFO);
				if (s.equals(Constants.CONNECTION_TIMEOUT) || s.equals("FAIL")) {
					msg.what = Constants.HTTP_REQUEST_ERROR;
				} else {
					try {
						JSONObject object = new JSONObject(s);
						Info info1 = new Info(object.getString("introduction"),
								object.getString("url"));

						msg.obj = info1;
						msg.what = Constants.HTTP_REQUEST_SUCCESS;
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						msg.what = Constants.HTTP_REQUEST_ERROR;
					}

				}
				handler.sendMessage(msg);
			}
		}).start();
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.title_back:
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			// overridePendingTransition(R.anim.in_from_right,
			// R.anim.out_to_left);
			// this.finish() ;
			onBackPressed();
			break;

		default:
			break;
		}
	}

	/*
	 * public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode ==
	 * KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
	 * 
	 * Intent intent = new Intent(this, MainActivity.class);
	 * overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
	 * startActivity(intent) ; finish();
	 * 
	 * return false; } return false; }
	 */
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		AppManager.getAppManager().finishActivity(this);
	}

}
